package com.oc.mba.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.oc.mba.entity.OcDevicePaper;
import com.oc.mba.entity.OcDevices;
import com.oc.mba.entity.OcPaper;
import com.oc.mba.entity.OcQuestion;
import com.oc.mba.service.DeviceService;
import com.oc.mba.service.PaperService;
import com.oc.mba.util.CommConsts;
import com.oc.mba.util.CommLog;
import com.oc.mba.util.CommUtil;
import com.oc.mba.util.JsonUtil;
import com.oc.mba.util.ResultCode;

/**
 * 试题控制
 * 
 * @author xiaoyu.wang
 */
@Controller
public class PaperController extends BaseController implements IController {
	private static final CommLog logger = new CommLog(PaperController.class);
	
	@Autowired
	private PaperService paperService;
	@Autowired
	private DeviceService deviceService;
	
	@Override
	public String progress(WrappedReq wReq, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.info("req=" + wReq);
		
		if (!wReq.getContent().containsKey(CommConsts.DEVICE_ID)) {
			Map<String, Object> result = new HashMap<String, Object>();
			result.put(CommConsts.RESULT_FLAG, ResultCode.INVALID_REQ);
			result.put(CommConsts.Result_DETAIL_FLAG, "Less Param");
			return JsonUtil.jsonMTS(result);
		}
		
		Integer deviceID = wReq.getContent().getInt(CommConsts.DEVICE_ID);
		OcDevices device = deviceService.findById(deviceID);
		// 无效设备
		if (device == null) {
			Map<String, Object> result = new HashMap<String, Object>();
			result.put(CommConsts.RESULT_FLAG, ResultCode.FAILURE);
			result.put(CommConsts.Result_DETAIL_FLAG, "Invalid Device");
			return JsonUtil.jsonMTS(result);
		}
		
		// 取得所有试题列表
		if (wReq.getSignID().equals(CommConsts.SIGNID_LIST_PAPERS)) {
			// 查找用户已经购买的试卷
			List<OcDevicePaper> devicePaperList = deviceService.findDevicePapers(deviceID);
			// 所有可见试题
			List<OcPaper> papers = paperService.list();
			List<Map<String, Object>> papersList = new ArrayList<Map<String, Object>>();
			// 拼装试卷，返回客户端
			for (OcPaper paper : papers) {
				Map<String, Object> paperMap = CommUtil.toMap(paper);
				paperMap.put("paid", false);
				paperMap.put("paperID", paper.getId());
				paperMap.put("questionNum", paper.getOcPaperQuestions().size());
				for (OcDevicePaper paidPaper : devicePaperList) {
					// 已经购买了试卷
					if (paidPaper.getPaperId() == paper.getId()) {
						paperMap.put("paid", true);
						break;
					}
				}
				
				papersList.add(paperMap);
			}
			
			Map<String, Object> result = new HashMap<String, Object>();
			result.put(CommConsts.RESULT_FLAG, ResultCode.SUCCESS);
			result.put("papers", papersList);
			return JsonUtil.jsonMTS(result);
		}
		// 下载试题
		else if (wReq.getSignID().equals(CommConsts.SIGNID_DOWNLOAD_PAPER)) {
			Integer paperID = wReq.getContent().getInt(CommConsts.PAPER_ID);
			
			// 取得试卷的试题列表
			List<OcQuestion> questionList = paperService.findQuestionByPaperID(paperID);
			List<Map<String, Object>> questionMapList = new ArrayList<Map<String, Object>>();
			for (OcQuestion ocQuestion : questionList) {
				questionMapList.add(CommUtil.toMap(ocQuestion));
			}
			Map<String, Object> result = new HashMap<String, Object>();
			result.put(CommConsts.RESULT_FLAG, ResultCode.SUCCESS);
			result.put("questionList", questionMapList);
			return JsonUtil.jsonMTS(result);
		}
		// 无效请求
		else {
			return genError(ResultCode.INVALID_REQ);
		}
	}
}
